package main

import (
	"fmt"
	"html/template"
	"math/rand"
	"net/http"
)

type Address struct {
	City     string
	Province string
}
type UserInfo struct {
	Name    string
	Sex     string
	Age     int
	Address Address
}

func login(w http.ResponseWriter, r *http.Request) {
	method := r.Method
	if method == "GET" {
		t, err := template.ParseFiles("./index.html")
		if err != nil {
			fmt.Fprintf(w, "load login.html falied")
			return
		}
		var userlist []*UserInfo
		for i := 0; i < 30; i++ {
			user := UserInfo{
				Name: fmt.Sprintf("Mary%d", rand.Intn(10000)),
				Sex:  "男",
				Age:  rand.Intn(100),
				Address: Address{
					City:     "北京",
					Province: "北京市",
				},
			}
			userlist = append(userlist, &user)
		}

		t.Execute(w, userlist)
	}
}
func main() {
	http.HandleFunc("/login", login)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Printf("listen server falied,err :%v\n", err)
		return
	}
}
